package com.guigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.guigu.common.utils.R;
import com.guigu.vod.dto.EquipmentVideo;
import com.guigu.vod.service.VodService;
import com.guigu.vod.utils.ConstantVodUtils;
import com.guigu.vod.utils.InitVodCilent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/vod/filevod")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频
    @PostMapping("uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file){
        String videoID = vodService.uploadVideoAly(file);
        System.out.println(videoID);
        return R.ok().data("videoID",videoID);
    }

    // 根据视频id删除视频（封装到业务接口）removeAlyVideo
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id){
        try {
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.ok();
    }

    //删除阿里云中多个视频
    @DeleteMapping("deletebatch")
    public R deletebatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.deleteById(videoIdList);
        return R.ok();
    }

    //根据视频id获取视频凭证（封装到业务接口）
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建requst对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            //远程调用：根据videoId,把播放次数加一

            return R.ok().data("playAuth",playAuth);
        }catch (Exception e){
           throw new RuntimeException("获取凭证失败");
        }
    }

    // 根据视频ID获取视频路径
    @GetMapping("getVideoUrlById/{videoId}")
    public R getVideoUrlById(@PathVariable String videoId) {
        try {
            String videoUrl = vodService.getVideoUrlById(videoId);
            if (videoUrl != null) {
                return R.ok().data("videoUrl", videoUrl);
            } else {
                return R.error().message("未找到该视频的播放地址");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("获取视频地址失败: " + e.getMessage());
        }
    }
}
